﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class EditClientFrm : BaseFrm
    {
        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 用户临时变量
        /// </summary>
        TB_Clients _Client = null;

        /// <summary>
        /// 是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        public EditClientFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void bindData(int id)
        {
            this._Client = this.BaseDataProxy.GetClient(id);
            if (this._Client != null)
            {
                this.txtBoxAddress.Text = this._Client.address;
                this.txtBoxContactMan.Text = this._Client.contactMan;
                this.txtBoxFax.Text = this._Client.fax;
                this.txtBoxFullName.Text = this._Client.fullName;
                this.txtBoxMemo.Text = this._Client.memo;
                this.txtBoxShortName.Text = this._Client.shortName;
                this.txtBoxTel.Text = this._Client.tel;
                this.txtBoxBankAccount.Text = this._Client.bankAccount;
            }
        }

        /// <summary>
        /// 重置窗体清除数据
        /// </summary>
        private void clearForm()
        {
            for (int i = 0; i < this.groupBox1.Controls.Count; i++)
            {
                if (this.groupBox1.Controls[i].Name.StartsWith("txtBox"))
                    this.groupBox1.Controls[i].Text = null;
            }
        }

        private void EditClientFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            if (this.EditMode)
                bindData(this.ID);
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            addNew();
        }

        private void addNew()
        {
            clearForm();
            this.EditMode = false;
            this.ID = 0;
            this._Client = null;
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBoxFullName.Text))
            {
                MessageBox.Show("请输入客户全称！");
                this.txtBoxFullName.Focus();
                return;
            }

            TB_Clients client = new TB_Clients();
            client.address = this.txtBoxAddress.Text;
            client.contactMan = this.txtBoxContactMan.Text;
            client.fax = this.txtBoxFax.Text;
            client.fullName = this.txtBoxFullName.Text;
            client.memo = this.txtBoxMemo.Text;
            client.shortName = this.txtBoxShortName.Text;
            client.tel = this.txtBoxTel.Text;
            client.bankAccount = this.txtBoxBankAccount.Text;

            Common.OperateType opType = Common.OperateType.Add;
            bool ok = false;
            if (this.EditMode)
            {
                client.id = this._Client.id;
                client.deleteFlag = this._Client.deleteFlag;
                opType = Common.OperateType.Update;

                ok = this.BaseDataProxy.UpdateClient(client);

            }
            else
            {
                client.deleteFlag = false;
                client.id = this.BaseDataProxy.AddClient(client);
                ok = client.id > 0;
            }

            if (ok)
            {
                this._Client = client;
                this.ID = client.id;
                this.EditMode = true;
                CommonEventArg<TB_Clients> arg = new CommonEventArg<TB_Clients>(client, opType);
                YY.Common.EventCommon.OnClientUdDGVEvnt(sender, arg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.EditMode && this.ID > 0)
                {
                    if (this.BaseDataProxy.DeleteClient(this.ID))
                    {
                        CommonEventArg<TB_Clients> arg = new CommonEventArg<TB_Clients>(this._Client, Common.OperateType.Delete);
                        YY.Common.EventCommon.OnClientUdDGVEvnt(sender, arg);
                        MessageBox.Show("客户资料已删除！");
                        addNew();
                    }
                }
            }
        }
    }
}
